﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using InfFront.InfoViews.Base;

namespace InfFront.InfoViews.WPF
{
    /// <summary>
    /// Interaction logic for EntrySelectWindow.xaml
    /// </summary>
    public partial class EntrySelectWindow : Window
    {
        #region Private Variables
        private InfoViewSource source;
        private InfoViewEntry selectedEntry;
        private bool rebuilding = false;
        #endregion Private Variables

        #region Construction/Initialization
        public EntrySelectWindow()
        {
            InitializeComponent();
        }

        public void SetSource(InfoViewSource source)
        {
            if (source == this.source)
                return;
            this.source = source;
            this.DataContext = source;

            Rebuild();
        }

        #endregion Construction/Initialization

        public InfoViewEntry SelectedEntry
        {
            get { return selectedEntry; }
        }

        public bool Execute(InfoViewSource source)
        {
            SetSource(source);
            var dialogResult = ShowDialog();
            SetSource(null);
            return dialogResult.HasValue && dialogResult.Value;
        }

        public void Rebuild()
        {
            rebuilding = true;
            try
            {
                dataGrid.ItemsSource = null;
                dataGrid.Columns.Clear();

                if (source == null)
                    return;

                dataGrid.ItemsSource = source.GetBindingSource();
            }
            finally
            {
                rebuilding = false;
            }
        }

        private void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
            e.Handled = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
            e.Handled = true;
        }

        private void dataGrid_AutoGeneratedColumns(object sender, EventArgs e)
        {
            foreach (var col in dataGrid.Columns)
            {
                var field = source.FieldByName((string)col.Header);
                if (field == null)
                    continue;
                col.Header = field.Title;
                col.IsReadOnly = field.IsReadOnly;
                BindingOperations.SetBinding(col, Microsoft.Windows.Controls.DataGridColumn.WidthProperty,
                    new Binding("VisualSettings.DataGridColumnWidth") { Source = field, Mode = BindingMode.TwoWay });
            }
        }

        private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DialogResult = true;
            Close();
            e.Handled = true;
        }

        private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (rebuilding)
                return;

            selectedEntry = null;
            if (e.AddedItems.Count > 0)
                selectedEntry = source.ExtractEntry(e.AddedItems[0]);
        }
    }
}
